from redis_operator import enter_paid
from rabbitmq_operator import rabbit_conn
import json
from handle_db import init_paid_order


def paid_order(ch, method, properties, body):
    print("进入了")
    body = json.loads(body)
    print("我是paid_order", body)
    print("我要将其状态修改为已付款")
    if init_paid_order(body):
        enter_paid(body)
        ch.basic_ack(delivery_tag=method.delivery_tag)
    else:
        ch.basic_nack(delivery_tag=method.delivery_tag)


def start_paid_consume():
    channel = rabbit_conn.channel()
    exchange = 'paid.exchange'
    queue = 'paid.queue'
    channel.exchange_declare(exchange=exchange,
                             exchange_type='topic',
                             durable=True)
    channel.queue_declare(queue=queue,
                          durable=True)
    channel.queue_bind(queue=queue, exchange=exchange,
                       routing_key="paid." + '*' + '.' + '*' + '.' + '*'
                                   + '.' + '*' + '.' + '*')
    channel.basic_qos(prefetch_count=1)
    channel.basic_consume(on_message_callback=paid_order,
                          queue=queue,
                          auto_ack=False)
    print("我是支付消费者，我开始消费了")
    channel.start_consuming()


start_paid_consume()
